'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:PersonnelCcsqCtrl
 * @description
 * # PersonnelCcsqCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('PersonnelCcsqCtrl', function ($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
    var id = $scope.process_id = $routeParams.id
    //获取出场人员列表
    $scope.getOutUsers = function () {
      Util.saveSelectLists($scope.param.details);
      Util.savaParamForReview($scope.param, id);
      $location.path("administrator/chooseinfo/ccsq");
    };
    // 删除出场人员列表
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
      Util.saveSelectLists($scope.param.details);
    };
    $scope.swipe = function (index) {
      $scope.index = index;
    }
    $scope.resetlist = function (index) {
      $scope.index = -1;
    }

    // 获取选择的人员
    var getAllUsers = function () {
      var users = [];
      users = $scope.param.details.map(function (item, index) {
        return {
          "ownerName": item.ownerName,
          "certificateType": item.certificateType,
          "attendanceCost": item.attendanceCost,
          "cerId": item.cerId,
          "remark": item.remark
        }
      });
      return JSON.stringify(users);
    };
    var checkUsers = function () {
      var check = true;
      $scope.param.details.forEach(function (item, index) {
        if (!item.attendanceCost && item.attendanceCost !== 0) {
          check = false;
          return false
        }
      })

      return check;
    }
    $scope.validate = function () {
      if (!$scope.param.attendanceDate) {
        Util.Toast("请选择出场日期");
        return;
      }
      if (!$scope.param.address) {
        Util.Toast("请选择出场地点");
        return;
      }
      if ($scope.param.relatedModuleName && $scope.param.relatedModuleName !== "无") {
        if (!$scope.param.moduleNo) {
          Util.Toast("请选择模块编号");
          return;
        }
      }
      if ($scope.param.details.length < 1) {
        Util.Toast("请添加出场人员");
        return;
      }
      var check = checkUsers();
      if (!check) {
        Util.Toast("请填写出场费用")
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    }
    // 提交申请
    $scope.submit = function (operationId) {
      $scope.param.attendanceDetails = getAllUsers();
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.attendanceApply;
      options.param = angular.copy($scope.param, {});
      delete options.param.details
      FormSubmit.submit(options, $scope);
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        // 获取选择页面返回的数据并且合并到参数中
        // 用来显示选择的报销明细
        var selectLists;
        selectLists = Util.getSelectLists();
        $scope.param.details = selectLists || $scope.param.details || [];
      });
    };
    // 初始化
    init();
  });